package pe.gob.mef.snip.preinversion.controller;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import pe.gob.mef.snip.preinversion.domain.Estudio;
import pe.gob.mef.snip.preinversion.domain.ProyectoBanco;
import pe.gob.mef.snip.preinversion.service.EstudioService;
import pe.gob.mef.snip.preinversion.service.ProyectoBancoService;
import pe.gob.mef.snip.preinversion.util.Parametros;
import pe.gob.mef.snip.preinversion.util.ServiceException;

@Controller
public class DocElectronicoController {

	protected final Log log = LogFactory.getLog(getClass());
	
	@Autowired
	private ProyectoBancoService proyectoBancoService;
	
	private ProyectoBanco proyectoBanco;
	
	@RequestMapping("/docElectronicoSnip03Listar")
	public ModelAndView docElectronicoSnip03Listar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("docElectronicoSnip03Listar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> docElectronicoSnip03Map = new HashMap<String, Object>();
		
	    String viewName = "";
		try{
			
			if(session.getAttribute("proyecto") == null || session.getAttribute("proyecto") == ""){
				viewName = "errorPage";
			}else{
				docElectronicoSnip03Map.put("proyecto", session.getAttribute("proyecto"));
				docElectronicoSnip03Map.put("version", session.getAttribute("version"));
				viewName = "frmDocElectronico";			
			}
			
			
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return new ModelAndView(viewName,"docElectronicoSnip03Map",docElectronicoSnip03Map);
		return new ModelAndView("frmDocElectronico","docElectronicoSnip03Map",docElectronicoSnip03Map);
	}
	
	
	@RequestMapping("/docElectronicoSnip03Grabar")
	//public @ResponseBody Map<String, String> docElectronicoSnip03Grabar(HttpServletRequest request, @RequestParam CommonsMultipartFile docElectronico, HttpServletResponse response) {
	public ModelAndView docElectronicoSnip03Grabar(HttpServletRequest request, @RequestParam CommonsMultipartFile docElectronico, HttpServletResponse response) {
				
		if (log.isDebugEnabled()) log.debug("docElectronicoSnip03Grabar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, String> docElectronicoSnip03Map = new HashMap<String, String>();
		
		proyectoBanco = new ProyectoBanco();
	    
		String viewName = "";
	       
		try{				
				docElectronicoSnip03Map.put("idProyecto", request.getParameter("idProyecto"));
				docElectronicoSnip03Map.put("version", request.getParameter("version"));
					
				//proyectoBanco.setProyecto(new BigDecimal((String)request.getParameter("idProyecto")));
				//proyectoBanco.setVersion(new BigDecimal((String)request.getParameter("version")));
				// ....
				
				if(docElectronico!=null && !docElectronico.getOriginalFilename().equals("")){
		              System.out.println(docElectronico.getOriginalFilename());
		         }
				
				//Para grabar en filesystem
				if(docElectronico!=null && !docElectronico.getOriginalFilename().equals("")){ 
					docElectronico.transferTo(new File(Parametros.FILE_SYSTEM + docElectronico.getOriginalFilename()));
		        }
		
				//proyectoBancoService.actualizar(proyectoBanco);
				
				viewName = "frmDocElectronico";			
		
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return docElectronicoSnip03Map;
		return new ModelAndView(viewName,"docElectronicoSnip03Map",docElectronicoSnip03Map);
		
	}
	

	@RequestMapping("/docElectronicoSnip04Listar")
	public ModelAndView docElectronicoSnip04Listar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("docElectronicoSnip04Listar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> docElectronicoSnip04Map = new HashMap<String, Object>();
		
	    String viewName = "";
		try{
			
			if(session.getAttribute("proyecto") == null || session.getAttribute("proyecto") == ""){
				viewName = "errorPage";
			}else{
				docElectronicoSnip04Map.put("proyecto", session.getAttribute("proyecto"));
				docElectronicoSnip04Map.put("version", session.getAttribute("version"));
				viewName = "frmDocElectronico";			
			}
			
			
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return new ModelAndView(viewName,"docElectronicoSnip04Map",docElectronicoSnip04Map);
		return new ModelAndView("frmDocElectronico","docElectronicoSnip04Map",docElectronicoSnip04Map);
	}
	
	
	@RequestMapping("/docElectronicoSnip04Grabar")
	public ModelAndView docElectronicoSnip04Grabar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("docElectronicoSnip04Grabar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> docElectronicoSnip04Map = new HashMap<String, Object>();
		
		proyectoBanco = new ProyectoBanco();
	    
		String viewName = "";
	       
		try{	
			
			if(session.getAttribute("proyecto") == null || session.getAttribute("proyecto") == ""){
				viewName = "errorPage";
			}else{
				docElectronicoSnip04Map.put("proyecto", session.getAttribute("proyecto"));
				docElectronicoSnip04Map.put("version", session.getAttribute("version"));
					
				proyectoBanco.setProyecto(new BigDecimal((String)request.getParameter("hd_proyecto")));
				proyectoBanco.setVersion(new BigDecimal((String)request.getParameter("hd_version")));
				// ....
				proyectoBancoService.actualizar(proyectoBanco);
				
				viewName = "frmDocElectronico";			
			}
		
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(viewName,"docElectronicoSnip04Map",docElectronicoSnip04Map);
		//return new ModelAndView(viewName,"docElectronicoSnip04Map",docElectronicoSnip04Map);

	}
	
	
	
}

